package cn.edu.suda.catalina.controller;

import cn.edu.suda.catalina.service.IndexService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by long on 16-11-28.
 */
@Controller
public class IndexController {

    @Autowired
    private IndexService indexService;

    /**
     * @param apkFile
     * @return
     * @throws IOException
     * app安装包上传
     */
    @RequestMapping("/upload")
    @ResponseBody
    public Map<String, Object> upload(HttpServletRequest request, @RequestParam("apk-file") MultipartFile apkFile) throws IOException {
        Map<String, Object> map = new HashMap<>();
        if (!apkFile.isEmpty()) {
            String path = request.getServletContext().getRealPath("/apps/");
            indexService.upload(path, apkFile);
            map.put("status", 1);
        } else {
            map.put("status", 0);
        }
        return map;
    }

    /**
     * @return
     */
    @RequestMapping("/selectAndroid")
    @ResponseBody
    public boolean selectAndroid(HttpServletRequest request) {
        String path = request.getServletContext().getRealPath("/img/");
        indexService.selectAndroid(path);
        return true;
    }

    @RequestMapping("/start")
    @ResponseBody
    public boolean start() {
        indexService.start();
        return true;
    }
}
